package com.example.gulimall.product.controller;

import com.alibaba.fastjson.JSONObject;
import com.example.common.entity.product.SkuInfoEntity;
import com.example.common.entity.product.SpuInfoEntity;
import com.example.common.to.kill.SkuInfoTo;
import com.example.common.utils.PageUtils;
import com.example.common.utils.R;
import com.example.gulimall.product.service.SkuInfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.List;
import java.util.Map;



/**
 * sku信息
 *
 * @author zyred
 * @email zyred@163.com
 * @date 2021-08-23 10:31:18
 */
@RestController
@RequestMapping("product/skuinfo")
public class SkuInfoController {
    @Autowired
    private SkuInfoService skuInfoService;

    @RequestMapping("/list")
    public R list(@RequestParam Map<String, Object> params){
        PageUtils page = skuInfoService.queryPage(params);
        return R.ok().put("page", page);
    }

    @PostMapping("/getNewPrice")
    public List<SkuInfoEntity> getNewPrice(@RequestBody List<Long> skuIds){
        return this.skuInfoService.getNewPrice(skuIds);
    }


    @GetMapping("/info/{skuId}")
    public R info(@PathVariable("skuId") Long skuId){
        return R.ok().put("skuInfo", JSONObject.toJSONString(skuInfoService.getById(skuId)));
    }

    @RequestMapping("/save")
    public R save(@RequestBody SkuInfoEntity skuInfo){
		skuInfoService.save(skuInfo);
        return R.ok();
    }

    @RequestMapping("/update")
    public R update(@RequestBody SkuInfoEntity skuInfo){
		skuInfoService.updateById(skuInfo);
        return R.ok();
    }

    @RequestMapping("/delete")
    public R delete(@RequestBody Long[] skuIds){
		skuInfoService.removeByIds(Arrays.asList(skuIds));
        return R.ok();
    }

    @PostMapping("/getSkuInfo")
    public List<SkuInfoTo> getSkuInfo (@RequestBody List<Long> skuIds) {
        return this.skuInfoService.getSkuInfo(skuIds);
    }

}
